package com.opc.Controller;

import com.opc.Common.Code;
import com.opc.Common.Result;
import com.opc.Config.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.IntStream;

@RestController
@Slf4j
@RequestMapping("/CacheUtil")
public class CacheUtilController {

//    @PostMapping("/homeCache")
//    public Result homeCache(@RequestBody HomeCache homeCache) {
//        log.info("缓存主页数据是"+homeCache);
//        CacheUtil.putInCache("screw"+ homeCache.getScrewTypeId(),homeCache.getTorque());
//        return new Result(Code.SAVE_OK,"缓存主页数据成功");
//    }

//    private final Executor asyncExecutor;

    //优化后（异步模式）：
    @GetMapping("/getHomeCache")
    @CrossOrigin
    @Async("cacheThreadPool")
    public CompletableFuture<Result> getHomeCache() {
        List<Object> homeCache = Collections.synchronizedList(new ArrayList<>());

        IntStream.rangeClosed(1, 20)
                .parallel()
                .forEach(count -> {
                    Object cache = CacheUtil.getFromCache("screw" + count);
                    if (cache != null) {
                        homeCache.add(cache);
                    }
                });

        if (homeCache.isEmpty()) {
            return CompletableFuture.completedFuture(new Result(Code.SELECT_ERR, "没有螺丝缓存"));
        }

        return CompletableFuture.completedFuture(new Result(Code.SELECT_OK, homeCache, "获取到了螺丝缓存"));
    }

    //---------------------------------
    //优化前（同步模式）：
    //反正车型一变就统一清理一次@RequestBody HomeCache homeCache
//    @GetMapping("/getHomeCache")
//    @CrossOrigin
//    public Result getHomeCache() {
//        log.info("getHomeCache的线程id:{}", Thread.currentThread().getId()+"名"+Thread.currentThread().getName());
//
////        log.info("获取主页数据是");
//        List<Object> homeCache = new ArrayList<>();
//
//        for (int count = 1; count <= 20; count++) {
//            Object cache = CacheUtil.getFromCache("screw"+ count);
//            if(cache != null){
//                homeCache.add(cache);
//            }
//        }
//        if(homeCache.isEmpty()){
//            return new Result(Code.SELECT_ERR,"没有螺丝缓存");
//        }
//        log.info("获取螺丝缓存数据:"+homeCache);
//        return new Result(Code.SELECT_OK,homeCache,"获取到了螺丝缓存");
//    }

    //获取铃铛告警数缓存
    @GetMapping("/getAlarmCache")
    @CrossOrigin
    public Result getAlarmCache() {
        log.info("getAlarmCache的线程id:{}", Thread.currentThread().getId()+"名"+Thread.currentThread().getName());

        log.info("获取铃铛告警数缓存");
        Object alarmCache = CacheUtil.getFromCache("alarmsTime");
        log.info("获取铃铛告警数缓存是"+alarmCache);
        if(alarmCache != null){
            int alarmCache1 = Integer.parseInt(alarmCache.toString());
            return new Result(Code.SELECT_OK,alarmCache1,"获取到了铃铛告警数缓存");
        }
        return new Result(Code.SELECT_ERR,"没有铃铛告警数缓存");
    }

    //获取控制器是否连接上的缓存
    @GetMapping("/ControllerCache")
    @CrossOrigin
    public Result ControllerCache() {
        System.out.println("每5秒执行一次");
        log.info("ControllerCache的线程id:{}", Thread.currentThread().getId()+"名"+Thread.currentThread().getName());
        log.info("获取控制器是否连接上的缓存");
        Object Controller1 = CacheUtil.getFromCache("controller1");
        Object Controller2 = CacheUtil.getFromCache("controller2");
        Object Controller3 = CacheUtil.getFromCache("controller3");
        Object Controller4 = CacheUtil.getFromCache("controller4");
        HashMap<String, String> controllerOnOrDown = new HashMap<>();
        controllerOnOrDown.put("controller1",Controller1.toString());
        controllerOnOrDown.put("controller2",Controller2.toString());
        controllerOnOrDown.put("controller3",Controller3.toString());
        controllerOnOrDown.put("controller4",Controller4.toString());

//        controllerOnOrDown.put("controller1","1");
//        controllerOnOrDown.put("controller2","1");
//        controllerOnOrDown.put("controller3","1");
//        controllerOnOrDown.put("controller4","1");
        log.info("获取控制器连接成功与否缓存"+Controller1+Controller2+Controller3+Controller4);
//        if(Controller != null){
//            Map<String, String> controllerOnOrDown = (Map<String, String>) Controller;
            return new Result(Code.SELECT_OK,controllerOnOrDown,"控制器连接成功与否缓存");
//        }
//        return new Result(Code.SELECT_ERR,"没有控制器连接成功与否缓存");
    }
}
